// index.js
var app = getApp()
Page({
  data: {
    navBarHeight: 44, // 默认导航栏高度
    menuButtonTop: 0, // 菜单按钮上边距
    menuButtonHeight: 32, // 菜单按钮高度
    testShow:true,
    keyword:'',
    gridList:[],
    page:1,
    more:'向上滑动查看更多'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     // 获取系统信息
     const systemInfo = wx.getSystemInfoSync()
     // 获取菜单按钮(胶囊按钮)的位置信息
     const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
     
     this.setData({
       statusBarHeight: systemInfo.statusBarHeight,
       menuButtonTop: menuButtonInfo.top,
       menuButtonHeight: menuButtonInfo.height,
       // 计算导航栏总高度 = (胶囊按钮上边距 - 状态栏高度) * 2 + 胶囊按钮高度
       navBarHeight: (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height
     })
  },

  getRandomInt() {
    return (350 + Math.random() * 200)
  },

  goods_list(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"srapi/home/goods_list",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      data:{
        keyword:that.data.keyword,page:that.data.page
      },
      dataType: 'json',
      success: function(re) {
        console.log(re)
        var goods = re.data.data.data
        if(goods.length==0){
          that.setData({
            more:'已加载全部内容'
          })
        }else{
          if(re.data.data.count<=10){
            that.setData({
              more:'已加载全部内容'
            })
          }else{
            that.setData({
              more:'向上滑动查看更多'
            })
          }
        }
        

        for(var i=0;i<goods.length;i++){
          goods[i]['height'] = that.getRandomInt()
        }

        that.setData({
          gridList:goods
        })
      }
    })
  },

  sub(){
    this.setData({
      page:1,more:'向上滑动查看更多'
    })
    this.goods_list()
  },

  keyword_action(e){
    this.setData({
      keyword:e.detail.value
    })
  },

  loadMoreData(){
    var that = this
    wx.request({
      url: getApp().globalData.httpUrl+"srapi/home/goods_list",
      header: {
        'content-type': 'application/x-www-form-urlencoded','appid':getApp().globalData.appId
      },
      method: 'POST',
      data:{
        keyword:that.data.keyword,page:that.data.page
      },
      dataType: 'json',
      success: function(re) {
        console.log(re)
        var goods = re.data.data.data
        if(goods.length==0){
          that.setData({
            more:'已加载全部内容'
          })
        }else{
          if(re.data.data.count<=10){
            that.setData({
              more:'已加载全部内容'
            })
          }else{
            that.setData({
              more:'向上滑动查看更多'
            })
          }
        }
        for(var i=0;i<goods.length;i++){
          goods[i]['height'] = that.getRandomInt()
        }

        that.setData({
          gridList:[...that.data.gridList,...goods]
        })
      }
    })
  },

  onReachBottom(){
    console.log('触发上拉加载更多');
  },

  loadMore() {
    console.log('触发上拉加载更多');
    this.setData({
      page:this.data.page+1
    })
    this.loadMoreData(); // 调用加载更多数据的方法
  },

  testHide(){
    this.setData({
      testShow:false
    })
  },


  info(e){
    wx.navigateTo({
      url: '/pages/info/index?id='+e.currentTarget.dataset.id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      page:1,more:'向上滑动查看更多'
    })
    this.goods_list()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
  
})
